﻿using StarryEdge.Core;
using StarryEdge.Core.Models;
using StarryEdge.Core.Models.Interact;
using StarryEdge.Core.Parameter;
using StarryEdge.Core.Tool;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Parameter
{
    public class SystemConfigParameter : InitFileBase, StarryEdge.Core.IInitFile, ISystemConfigParameter
    {
        public SystemConfigParameterModel SystemConfig { get; set; }
        public ObservableCollection<PLCResectionModel> PLCResectionList { get; set; }

        #region CIP
        public ObservableCollection<InteractAddressModel<string>> StringIAddressList { get; set; }
        public ObservableCollection<ScanSignalModel<string>> StringSignalList { get; set; }
        #endregion

        #region Address
        public ObservableCollection<InteractAddressModel<int>> AddressIAddressList { get; set; }
        public ObservableCollection<ScanSignalModel<int>> AddressSignalList { get; set; }
        #endregion

        public SystemConfigParameter(IStarryEdgeLog edgeLog) : base(edgeLog, "init", "systemconfig")
        {
            Init();
        }

        public void Init()
        {
            if (File.Exists(_path))
            {
                string str = base.Read();
                if (!string.IsNullOrEmpty(str))
                {
                    Dictionary<string, object> keyValues = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(str);
                    try
                    {
                        AddressIAddressList = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<InteractAddressModel<int>>>(keyValues["AddressIAddressList"].ToString());
                        AddressSignalList = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<ScanSignalModel<int>>>(keyValues["AddressSignalList"].ToString());
                        StringIAddressList = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<InteractAddressModel<string>>>(keyValues["StringIAddressList"].ToString());
                        StringSignalList = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<ScanSignalModel<string>>>(keyValues["StringSignalList"].ToString());
                        SystemConfig = System.Text.Json.JsonSerializer.Deserialize<SystemConfigParameterModel>(keyValues["SystemConfig"].ToString());
                        PLCResectionList = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<PLCResectionModel>>(keyValues["PLCResectionList"].ToString());
                    }
                    catch (Exception ex)
                    {
                        _runLog.LogRun($"系统参数初始化异常：{ex}", Core.Enums.LogLevel.Error);
                    }
                }
            }
            if (AddressIAddressList == null)
            {
                AddressIAddressList = new ObservableCollection<InteractAddressModel<int>>();
            }
            if (AddressSignalList == null)
            {
                AddressSignalList = new ObservableCollection<ScanSignalModel<int>>();
            }
            if (StringIAddressList == null)
            {
                StringIAddressList = new ObservableCollection<InteractAddressModel<string>>();
            }
            if (StringSignalList == null)
            {
                StringSignalList = new ObservableCollection<ScanSignalModel<string>>();
            }
            if (PLCResectionList == null)
            {
                PLCResectionList = new ObservableCollection<PLCResectionModel>();
            }
            if (SystemConfig == null)
            {
                SystemConfig = new SystemConfigParameterModel();
            }
        }

        public bool Save()
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("AddressIAddressList", AddressIAddressList);
            data.Add("AddressSignalList", AddressSignalList);
            data.Add("StringIAddressList", StringIAddressList);
            data.Add("StringSignalList", StringSignalList);
            data.Add("PLCResectionList", PLCResectionList);
            data.Add("SystemConfig", SystemConfig);
            return Save(System.Text.Json.JsonSerializer.Serialize(data, OptionsHelper.Options));
        }

        public SystemConfigParameterModel GetParameter()
        {
            return SystemConfig;
        }
    }
}
